import { Component, OnInit, OnDestroy } from '@angular/core';
import { ConfigService } from '../../service/app.config.service';
import { AppConfig } from '../../api/appconfig';
import { Subscription } from 'rxjs';
import { Store } from '@ngrx/store';
import { setUser } from 'src/app/store/user/user.actions';
import { AccountService } from 'src/app/service/account.service';
@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styles:[`
    :host ::ng-deep .p-password input {
    width: 100%;
    padding:1rem;
    }

    :host ::ng-deep .pi-eye{
      transform:scale(1.6);
      margin-right: 1rem;
      color: var(--primary-color) !important;
    }

    :host ::ng-deep .pi-eye-slash{
      transform:scale(1.6);
      margin-right: 1rem;
      color: var(--primary-color) !important;
    }
  `]
})
export class LoginComponent implements OnInit, OnDestroy {

  valCheck: string[] = ['remember'];

  username:string;

  password: string;
  
  config: AppConfig;
  
  subscription: Subscription;

  constructor(public configService: ConfigService,private store:Store,private account:AccountService){ }

  ngOnInit(): void {
    this.config = this.configService.config;
    this.subscription = this.configService.configUpdate$.subscribe(config => {
      this.config = config;
    });
  }

  ngOnDestroy(): void {
    if(this.subscription){
      this.subscription.unsubscribe();
    }
  }

  async onClick(){
    const { message, data: user } = await this.account.loginAccount({ username:this.username, password:this.password })
    console.log(user)
    localStorage.setItem('user',JSON.stringify(user))
    //Login({user:{user:'1234'}})
    this.store.dispatch(setUser({user:user}))
  }
}
